<?php

/**
 * 
 * Enter description here ...
 * @author ysahn
 *
 */
class Service_GenericSeedniaServiceStub extends Service_RestServiceStubBase
{
	
	// localhost:8080/seednia-webapp/ws/<modulename>ServiceRest/<modulename>/
	// contentType = application/json, application/x-www-form-urlencoded
	public function __construct($serviceUriRoot, $accessToken, $contentType = 'application/x-www-form-urlencoded')
	{
		parent::__construct($serviceUriRoot, $accessToken, $contentType);
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param string $accessToken
	 * @param long $xid
	 * @return object
	 */
	public function getByXid($xid, $path = "", $extraQueryParam = NULL)
	{
		$reqParamArr = array('accessToken' => $this->_accessToken);
		
		if ($extraQueryParam != NULL)
			$reqParamArr = array_merge($reqParamArr, $extraQueryParam);
		if ($path != NULL && strlen($path) > 0)
		{
			$path = $path."/";
		}
	
		$response = $this->_restClient->get($path.$xid.'/?'. http_build_query($reqParamArr));
		
		return $this->handleGetResonse($response);;
	}
	
	/**
	*
	* Enter description here ...
	* @param string $accessToken
	* @param array $criteria an array of parameters
	* @return object
	*/
	public function get($criteria, $path = "")
	{
		$reqParamArr = array_merge(array('accessToken' => $this->_accessToken), $criteria);
		if ($path != NULL && strlen($path) > 0)
		{
			$path = $path."/";
		}
		$response = $this->_restClient->get($path.'?'. http_build_query($reqParamArr));
		
		return $this->handleGetResonse($response);
	}

	/**
	 * Retrieves a list of entries from the resource
	 * @param array $criteria
	 * @param int $offset
	 * @param int $max
	 * @param string $path (does not start with /)
	 * @return type 
	 */
	public function getList($criteria, $offset, $max, $path = "")
	{
		
		$queryParms = array('offset' => $offset, 'max' => $max);
		$reqParamArr = 
				array('accessToken' => $this->_accessToken 
					, 'criteria' => json_encode($criteria)
					, 'parms' => json_encode($queryParms) );
			
		$response = $this->_restClient->get($path.'?'. http_build_query($reqParamArr));
		
		return $this->handleGetResonse($response);
	}
	
	/**
	 * Adds (inserts) an entry to to the resource
	 * @param string $body
	 * @param array $fields
	 */
	public function add($fields, $path = "")
	{
		if (!array_key_exists('creatorSid', $fields)) {
			if ($this->getUserSid() != null) {
			$fields['creatorSid'] = $this->getUserSid();
			}
		}
		if (!array_key_exists('domainId', $fields) && $this->getDomainId() != null)
			$fields['domainId'] = $this->getDomainId();
		
		/*
		$postParams = array();
		$postParams['record'] = json_encode($fields);
		
		$response = $this->_restClient->post($path."?".http_build_query($reqParamArr), http_build_query($postParams));
		
		$result = null;
		if ($response->status == REST_Client::HTTP_OK)
			$result = json_decode($response->data);
		return $result;
		 */
		return $this->postJson(json_encode($fields), $path);
	}
	
	public function postJson($recordInJson, $path ='')
	{
		$reqParamArr = array('accessToken' => $this->_accessToken);

		$postParams = array();
		$postParams['record'] = $recordInJson;
		$response = $this->_restClient->post($path."?".http_build_query($reqParamArr), http_build_query($postParams));
		
		$result = null;
		if ($response->status == REST_Client::HTTP_OK)
			$result = json_decode($response->data);
		return $result;
	}
	
	/**
	 *
	 * @param array $fields the fields are converted into json
	 * @param string $path context path (does not start with /)
	 * @return type 
	 */
	public function update($fields, $path = "")
	{
		$reqParamArr = array('accessToken' => $this->_accessToken);
		
		$formParams = array('record' => json_encode($fields));
		
		$response = $this->_restClient->put($path."?".http_build_query($reqParamArr), http_build_query($formParams));
		
		$result = null;
		if ($response->status == REST_Client::HTTP_OK)
			$result = json_decode($response->data);
		return $result;
	}
	
	/**
	 *
	 * @param type $putParams
	 * @param type $path does not start with /
	 * @return type 
	 */
	public function updateRaw($putParams, $path = "")
	{
		$reqParamArr = array('accessToken' => $this->_accessToken);
		
		$response = $this->_restClient->put($path."?".http_build_query($reqParamArr), http_build_query($putParams));
		
		$result = null;
		if ($response->status == REST_Client::HTTP_OK)
			$result = json_decode($response->data);
		return $result;
	}
	
	public function deleteBySid($sid, $path = "", $extraQueryParam = NULL)
	{
		$reqParamArr = array('accessToken' => $this->_accessToken);
		$reqParamArr['criteria'] = json_encode( array('sid' => $sid));
		
		if ($extraQueryParam != NULL)
			$reqParamArr = array_merge($reqParamArr, $extraQueryParam);
		if ($path != NULL && strlen($path) > 0)
		{
			$path = $path."/";
		}
	
		$response = $this->_restClient->delete($path.'?'. http_build_query($reqParamArr));
		
		return $this->handleGetResonse($response);;
	}
	
	/**
	 * Deprecated since started using Jackon in Java server side
	 * @param type $response
	 * @return null
	 * @throws Exception 
	 */
	private function handleGetResonse(&$response)
	{
		$retval = null;
		if ($response->status == REST_Client::HTTP_OK) {
			$retval = json_decode($response->data);
			/*
			if (is_object($retval) ) {
				if (isset($retval->{$this->_resultNodeName})) {
					$retval = $retval->{$this->_resultNodeName};
				}
			}*/
		}
		else if ($response->status == REST_Client::HTTP_NOCONTENT) { // No content
			$retval = null; // Everything OK, just return null
		}
		else {
			throw new Exception("HTTP Status: ".$response->status);
		}
		
		return $retval;
	}
}